package com.mls.fivedistrict.util.jedis;


import com.mls.fivedistrict.common.RedisPool;
import lombok.extern.slf4j.Slf4j;
import redis.clients.jedis.Jedis;

import java.util.Set;

/**
 * Created by geely
 */
@Slf4j
public class RedisPoolUtil {


    /**
     * 设置key的有效期，单位是秒
     *
     * @param key
     * @param exTime
     * @return
     */
    public static Long expire(String key, int exTime) {
        Jedis jedis = null;
        Long result = null;
        try {
            jedis = RedisPool.getJedis();
            result = jedis.expire(key, exTime);
        } catch (Exception e) {

            RedisPool.returnBrokenResource(jedis);
            return result;
        }
        RedisPool.returnResource(jedis);
        return result;
    }

    //exTime的单位是秒
    public static String setEx(String key, String value, int exTime) {
        Jedis jedis = null;
        String result = null;
        try {
            jedis = RedisPool.getJedis();
            result = jedis.setex(key, exTime, value);
        } catch (Exception e) {

            RedisPool.returnBrokenResource(jedis);
            return result;
        }
        RedisPool.returnResource(jedis);
        return result;
    }

    public static String set(String key, String value) {
        Jedis jedis = null;
        String result = null;

        try {
            jedis = RedisPool.getJedis();
            result = jedis.set(key, value);
        } catch (Exception e) {

            RedisPool.returnBrokenResource(jedis);
            return result;
        }
        RedisPool.returnResource(jedis);
        return result;
    }

    public static String get(String key) {
        Jedis jedis = null;
        String result = null;
        try {
            jedis = RedisPool.getJedis();

            result = jedis.get(key);
        } catch (Exception e) {

            RedisPool.returnBrokenResource(jedis);
            return result;
        }
        RedisPool.returnResource(jedis);
        return result;
    }

    public static Long del(String key) {
        Jedis jedis = null;
        Long result = null;
        try {
            jedis = RedisPool.getJedis();
            result = jedis.del(key);
        } catch (Exception e) {

            RedisPool.returnBrokenResource(jedis);
            return result;
        }
        RedisPool.returnResource(jedis);
        return result;
    }

    public static Set<String> getKeys() {
        Jedis jedis = null;
        Set<String> keys = null;
        try {
            jedis = RedisPool.getJedis();
            keys = jedis.keys("*");
        } catch (Exception e) {
            RedisPool.returnBrokenResource(jedis);
            return keys;
        }
        RedisPool.returnResource(jedis);
        return keys;
    }

    public static void main(String[] args) {
        Jedis jedis = RedisPool.getJedis();
        RedisShardedPoolUtil.set("keyTest", "value");

//        String value = RedisShardedPoolUtil.get("keyTest");
//
//        RedisShardedPoolUtil.setEx("keyex","valueex",60*10);
//
//        RedisShardedPoolUtil.expire("keyTest",60*20);
//
//        RedisShardedPoolUtil.del("keyTest");
//
//
//        String aaa = RedisShardedPoolUtil.get(null);
//        System.out.println(aaa);
//
//        System.out.println("end");


    }


}
